<template>
  <div>
    <van-nav-bar title="登陆" class="navbar" />
    <div>
      <van-form @submit="onSubmit" class="form" ref="form">
        <!-- 手机号 -->
        <van-field
          v-model="mobile"
          name="mobile"
          placeholder="手机号"
          :rules="mobileRuler"
        >
          <template #label>
            <span class="toutiao toutiao-shouji"></span>
          </template>
        </van-field>

        <!-- 验证码 -->
        <van-field
          v-model="code"
          name="code"
          placeholder="验证码"
          :rules="codeRuler"
        >
          <template #label>
            <span class="toutiao toutiao-yanzhengma"></span>
          </template>

          <!-- 获取验证码 -->
          <template #button>
            <van-button
              v-if="isShowCodeBtn"
              class="code-btn"
              type="default"
              size="small"
              round
              native-type="button"
              @click="getCode"
              >获取验证码</van-button
            >
            <van-count-down
              v-else
              :time="60 * 1000"
              format="ss秒"
              @finish="isShowCodeBtn = true"
            />
          </template>
        </van-field>
        <div style="margin: 16px">
          <van-button block type="info" native-type="submit">登陆</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { loginAPI, getCodeAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  data() {
    const mobileRuler = [
      { required: true, message: '请填写手机号' },
      {
        pattern:
          /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
        message: '手机号格式非法'
      }
    ]
    const codeRuler = [
      { required: true, message: '请填写验证码' },
      { pattern: /^[0-9]{6}$/, message: '请输入六位验证码' }
    ]
    return {
      mobile: '',
      code: '',
      mobileRuler,
      codeRuler,
      isShowCodeBtn: true
    }
  },
  methods: {
    ...mapMutations(['SET_TOKEN']),
    async onSubmit() {
      // console.log("submit", values);
      const { code, mobile } = this

      // 组件
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })

      try {
        const { data } = await loginAPI(mobile, code)
        // console.log(data)
        this.SET_TOKEN(data.data)
        // console.log(data.data)
        this.$router.push('/profile')
        this.$toast.success('登陆成功')
      } catch (error) {
        if ((error, response?.status === 400)) {
          this.$toast.fail(error.response.data.message)
        } else {
          throw error
        }
      }
    },
    async getCode() {
      // 给form表单绑定ref
      // 传入的name是属性  name写的是mobile
      await this.$refs.form.validate('mobile')
      // 显示倒计时
      this.isShowCodeBtn = false
      this.$toast.loading({
        message: '发送验证码中......',
        forbidClick: true
      })

      try {
        await getCodeAPI(this.mobile)
        this.$toast.success('发送验证码成功！')
      } catch (error) {
        if (error.response?.status === 429) {
          this.$toast.fail(error.response.data.message)
        } else {
          throw error
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.navbar {
  background-color: #3298fa;

  :deep(.van-nav-bar__title) {
    color: #fff;
  }
}
//  穿透 覆盖样式
:deep(.form) {
  .toutiao {
    font-size: 40px;
  }
  .van-field__label {
    flex: 1;
  }
  .van-field__value {
    flex: 20;
  }
}
</style>
